<?php
session_start(); //we need to call PHP's session object to access it through CI
class Laporan extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->data['title'] = 'Laporan';
	}

	function index()
	{
		$this->load();
		$this->load->model('m_ujian');
		$date = $this->get_indo_date();
		$where = array(array('ujian.uj_date <' => $date[0]), array('ujian.uj_date =' => $date[0], 'ujian.uj_start <' =>$date[1]));
		$this->data['rows'] = $this->m_ujian->get_past_ujian($where);
		$this->load->library('view');
		$this->view->set_menu('laporan');
		$this->view->set_portlet_title('Laporan');
		$this->view->set_portlet_style('portlet box red');
		$this->view->set_view('custom/laporan');
		$this->view->show($this->data);
	}
	
	function get_indo_date(){
		$date = gmdate('d-m-Y H:i:s');
		$currentDate = strtotime($date);
		$futureDate = $currentDate+(60*60*7);
		$formatDate = date("Y-m-d H:i:s", $futureDate);
		return explode(' ', $formatDate);
	}
	
	function detail($id)
	{
		$this->load();
		$this->load->model('m_ujian');
		$ujian = $this->m_ujian->get_data_with_mp(array('uj_id'=>$id));
		$this->data['rows'] = $this->m_ujian->get_detail($id);
		$this->data['id'] = $id;
		$this->load->library('view');
		$this->view->set_menu('laporan');
		$this->view->set_portlet_title('Detail Laporan '.$ujian[0]->uj_name.' - '.$ujian[0]->mp_name);
		$this->view->set_portlet_style('portlet box red');
		$this->view->set_view('custom/laporan_detail');
		$this->view->show($this->data);
	}
	
	function show_result_siswa($id, $sw_id){
		$this->load();			
		$this->load->model('m_nilai');
		$this->load->model('m_ujian');
		$this->load->model('m_jawaban');
		$rows = $this->m_nilai->get_data(array('uj_id' => $id, 'sw_id' => $sw_id));

		$this->data['title'] = 'Hasil Ujian';
		$this->data['soal'] = $this->m_ujian->get_ujian_with_soal($id, array('jawaban.ja_stat = "1"'));
		$this->data['nilai'] = isset($rows[0]->nilai)?$rows[0]->nilai:0;
		$this->data['jawaban'] = array();
		$this->data['return'] = 'laporan/detail/'.$id;

		if(count($rows)>1):
			$soal = explode(',', $rows[0]->soal);
			asort($soal);
			$ja = explode(',', $rows[0]->jawaban);
			$jawaban = $this->m_jawaban->get_data_in($ja);
			$i = 0;
			if(count($jawaban)):
				foreach ($soal as $row){
					$this->data['jawaban'][$row]['desc'] = $jawaban[$i]['ja_desc'];
					$this->data['jawaban'][$row]['stat'] = $jawaban[$i]['ja_stat'];
					$i++;
				}
			endif;
		endif;

		$this->load->library('view');
		$this->view->set_menu('laporan');
		$this->view->set_portlet_title('Hasil '.$this->data['soal'][0]->uj_name.' - '.$this->data['soal'][0]->mp_name);
		$this->view->set_portlet_style('portlet box green');
		$this->view->set_view('siswa/siswa_result');
		$this->view->show($this->data);
	}
	
	function export($id)
	{
		$this->load->model('m_ujian');
		$ujian = $this->m_ujian->get_data_with_mp(array('uj_id'=>$id));
		$this->data['rows'] = $this->m_ujian->get_detail($id);
		$date = date("d-m-Y", strtotime($ujian[0]->uj_date));
		$name = "Laporan-".$ujian[0]->uj_name.'-'.$ujian[0]->mp_name."(".$date.")";
		$this->data['name'] = str_replace(' ', '-', $name);
		$this->load->view('custom/export_laporan', $this->data);
	}
	
	function load(){
		if($this->session->userdata('admin')){
			$session_data = $this->session->userdata('admin');
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
		}else{
			redirect('admin', 'refresh');
		}
	}
}
?>